


<!DOCTYPE html>
<html id="htmlId">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
  <title>Coverage Report > JwtTokenUtil</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">com.ie.common.security.utils</a>
</div>

<h1>Coverage Summary for Class: JwtTokenUtil (com.ie.common.security.utils)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Branch, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">JwtTokenUtil</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    20%
  </span>
  <span class="absValue">
    (1/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    5%
  </span>
  <span class="absValue">
    (1/20)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package com.ie.common.security.utils;
&nbsp;
&nbsp;import cn.hutool.core.date.DateField;
&nbsp;import cn.hutool.core.date.DateTime;
&nbsp;import cn.hutool.jwt.JWTPayload;
&nbsp;import cn.hutool.jwt.JWTUtil;
&nbsp;import org.springframework.beans.factory.annotation.Autowired;
&nbsp;import org.springframework.stereotype.Component;
&nbsp;
&nbsp;import java.util.HashMap;
&nbsp;import java.util.Map;
&nbsp;
&nbsp;/**
&nbsp; * @classname: JwtTokenUtil
&nbsp; * @Author: xujia
&nbsp; * @Description: hutool工具类token操作方法
&nbsp; * @Date: 2024/9/8  22:37
&nbsp; * @Param:
&nbsp; * @version: 1.0
&nbsp; **/
&nbsp;
&nbsp;@Component//https://blog.csdn.net/qq_35430000/article/details/108910387
<b class="fc">&nbsp;public class JwtTokenUtil {</b>
&nbsp;    @Autowired
&nbsp;    private ConfigrarionBean myConfig;
&nbsp;    @Autowired
&nbsp;    private RedisUtil redisUtil;
&nbsp;    /**
&nbsp;     * @Description: 获取当前时间并计算过期时间，基于myConfig.getExpiration()分钟数。
&nbsp;     * 创建一个用于存储JWT载荷的Map对象。
&nbsp;     * 在载荷中存入签发人(iss)、签发时间(iat)、过期时间(exp)、生效时间(nbf)以及用户名。
&nbsp;     * 使用JWTUtil.createToken()方法和密钥(myConfig.getSecret())生成JWT令牌。
&nbsp;     * 返回生成的JWT令牌。
&nbsp;     * 函数返回的token是一个包含用户信息和时效信息的加密字符串，可用于用户身份验证和授权过程。
&nbsp;     * @Author: xujia
&nbsp;     * @Date: 2024/8/29  22:07
&nbsp;     * @Param: [userName]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: java.lang.String
&nbsp;     **/
&nbsp;    public String createToken(String userName) {
<b class="nc">&nbsp;        DateTime now = DateTime.now();</b>
<b class="nc">&nbsp;        DateTime newTime = now.offsetNew(DateField.MINUTE, myConfig.getExpiration());</b>
<b class="nc">&nbsp;        Map&lt;String,Object&gt; payload = new HashMap&lt;&gt;();</b>
&nbsp;        // 签发人
<b class="nc">&nbsp;        payload.put(JWTPayload.ISSUER, myConfig.getIss());</b>
&nbsp;        //签发时间
<b class="nc">&nbsp;        payload.put(JWTPayload.ISSUED_AT, now);</b>
&nbsp;        //过期时间
<b class="nc">&nbsp;        payload.put(JWTPayload.EXPIRES_AT, newTime);</b>
&nbsp;        //生效时间
<b class="nc">&nbsp;        payload.put(JWTPayload.NOT_BEFORE, now);</b>
&nbsp;        //载荷
<b class="nc">&nbsp;        payload.put(HttpJwtTokenUtil.KEY_USERNAME, userName);</b>
<b class="nc">&nbsp;        String token = JWTUtil.createToken(payload, myConfig.getSecret().getBytes());</b>
<b class="nc">&nbsp;        return token;</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 验证HTTP请求中携带的令牌是否有效
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  11:59
&nbsp;     * @Param: [token]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean
&nbsp;     **/
&nbsp;    public boolean verify(String token) {
&nbsp;
<b class="nc">&nbsp;        return JWTUtil.verify(token, myConfig.getSecret().getBytes());</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 根据令牌获取用户名
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  11:59
&nbsp;     * @Param: [token]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: java.lang.String
&nbsp;     **/
&nbsp;    public String getUsername(String token) {
<b class="nc">&nbsp;        return JWTUtil.parseToken(token).getPayload(HttpJwtTokenUtil.KEY_USERNAME).toString();</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 根据令牌是否过期，如果过期则更新redis中存储的令牌信息，并返回true，否则返回false。
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  12:00
&nbsp;     * @Param: [token]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean
&nbsp;     **/
&nbsp;    public boolean isExpiration(String token) {
<b class="nc">&nbsp;        String userName = getUsername(token);</b>
<b class="nc">&nbsp;        String key = &quot;login_&quot; + userName;</b>
<b class="nc">&nbsp;        boolean hasEx = redisUtil.hasKey(key);</b>
<b class="nc">&nbsp;        if(hasEx) {</b>
<b class="nc">&nbsp;            redisUtil.expire(key, myConfig.getRedisExpiration());</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } else {
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2025-06-15 18:23</div>
</div>
</body>
</html>
